﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

//#if UNITY_EDITOR
//using UnityEditor;
//[CustomEditor(typeof(HairUnitBaker))]
//public class HairUnitBakerEditor : Editor
//{
//    public override void OnInspectorGUI()
//    {
//        var o = (HairUnitBaker)target;
//        base.OnInspectorGUI();
//        if (GUILayout.Button("ScaleMesh"))
//        {
//            o.ScaleMesh();
//        }
//        if (GUILayout.Button("ScaleBone"))
//        {
//            o.ScaleBone();
//        }
//    }
//}
//#endif

public class HairUnitBaker : MonoBehaviour
{
    SkinnedMeshRenderer smr;
    public Transform[] bones;
    public float radius;
    public bool bDrawGizmos;
    void DrawAxis(Axis axis, Vector3 pos, float length, Color color)
    {
        Gizmos.color = color;
        Gizmos.DrawLine(pos, pos + axis.up.normalized * length);
        Gizmos.DrawLine(pos, pos + axis.right.normalized * length);
        Gizmos.DrawLine(pos, pos + axis.forward.normalized * length);
    }
    private void OnDrawGizmos()
    {
        if (!bDrawGizmos || bones == null || bones.Length ==0) return;
        foreach (var bone in bones)
        {
            var tip = bone.GetChild(0);
            Gizmos.color = Color.white;
            Gizmos.DrawSphere(bone.position, radius);
            Gizmos.DrawLine(bone.position, tip.position);
        }
    }
    public struct Axis
    {
        public Vector3 up;
        public Vector3 right;
        public Vector3 forward;
        public Axis(Vector3 scale)
        {
            up = Vector3.up * scale.y;
            right = Vector3.right * scale.x;
            forward = Vector3.forward * scale.z;
        }
        public static implicit operator Axis(Vector3 scale)
        {
            return new Axis(scale);
        }
        public static implicit operator Vector3(Axis axis)
        {
            return new Vector3(axis.right.x, axis.up.y, axis.forward.z);
        }
        public static Axis operator *(Matrix4x4 matrix, Axis axis)
        {
            Axis n;
            n.up = matrix.MultiplyPoint3x4(axis.up);
            n.right = matrix.MultiplyPoint3x4(axis.right);
            n.forward = matrix.MultiplyPoint3x4(axis.forward);
            return n;
        }
    }
    public void ScaleMesh(Mesh meshO)
    {
        //var mesh = Instantiate(meshO);
        //smr.sharedMesh = mesh;
        //var verts = mesh.vertices;
        //Vector3 worldScale, localScale;
        //worldScale = scale.toX1Z();

        //float x, z;
        //x = worldScale.x;
        //z = worldScale.z;

        //float localX, localY, localZ;
        //localX = z;
        //localY = x;
        //localZ = 1;
        //localScale = new Vector3(localX, localY, localZ);

        //for (int i = 0; i < verts.Length; i++)
        //{
        //    verts[i] = Vector3.Scale(verts[i], localScale);
        //}
        //mesh.vertices = verts;
    }
    public void ScaleBone(float v)
    {
        Vector3 localPos;
        Vector3 tipDir;
        Transform child;
        for (int i = 0; i < bones.Length; i++)
        {
            child = bones[i].GetChild(0);
            tipDir = child.position - bones[i].position;
            tipDir = tipDir.normalized;
            //localPos = tipDir * originLength[i] * scale.y;
            localPos = tipDir * v;
            child.position = bones[i].position + localPos;
        }
    }
    //public float[] originLength;
    Vector3[] originVerts;
    public void Init()
    {
        smr = GetComponentInChildren<SkinnedMeshRenderer>();
        bones = smr.bones;
        originVerts = smr.sharedMesh.vertices;
        //originLength = new float[bones.Length];
        //for (int i = 0; i < bones.Length; i++)
        //{
        //    originLength[i] = Vector3.Distance(bones[i].GetChild(0).position, bones[i].position);
        //}
    }
}
